package open.witype.filetiper.custom.Slidingmenu;

import android.app.Activity;
import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.RelativeLayout;

import open.witype.filetiper.R;

/**
 * Created by Typer on 2015/5/10 0010.
 * 侧滑菜单的父类容器
 */
public class SlidingMenuView extends RelativeLayout {

    private AboveViewContent mAboveViewContent;

    private BehindViewContent mBehindContent;

    public SlidingMenuView(Context context) {
        this(context, null);
    }

    public SlidingMenuView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public SlidingMenuView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
//        int childCount = getChildCount();
//        if (childCount == 2){
//            if(getChildAt(0) instanceof BehindViewContent){
//                mBehindContent = (BehindViewContent)getChildAt(0);
//            }
//            if(getChildAt(0) instanceof AboveViewContent){
//                mAboveViewContent = (AboveViewContent)getChildAt(1);
//            }
//        }else {
//
//        }
        LayoutParams behindParams = new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        mBehindContent = new BehindViewContent(context);
        addView(mBehindContent, behindParams);
        mAboveViewContent = new AboveViewContent(context);
        LayoutParams aboveParams = new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        addView(mAboveViewContent, aboveParams);
        mBehindContent.setAboveContent(mAboveViewContent);
        mAboveViewContent.setBehindContent(mBehindContent);

        TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.SlidingMenu);
        int viewAbove = ta.getResourceId(R.styleable.SlidingMenu_viewAbove, -1);

        if (viewAbove != -1) {
            setContent(viewAbove);
        } else {
            setContent(new FrameLayout(context));
        }
        int viewBehind = ta.getResourceId(R.styleable.SlidingMenu_viewBehind, -1);
        if (viewBehind != -1) {
            setMenu(viewBehind);
        } else {
            setMenu(new FrameLayout(context));
        }

        int firstOffset = (int) ta.getDimension(R.styleable.SlidingMenu_firstOffset, -1);
        int secondaryOffset = (int) ta.getDimension(R.styleable.SlidingMenu_secondaryOffset, -1);

//        if (firstOffset != -1 && secondaryOffset != -1)
//            throw new IllegalStateException("Cannot set both behindOffset and behindWidth for a SlidingMenu");
        if (firstOffset != -1)
            mBehindContent.setWidthOffset(firstOffset);
        if (secondaryOffset != -1)
            mBehindContent.setmSecondaryWidthOffset(secondaryOffset);
        else
            mBehindContent.setWidthOffset(0);
        ta.recycle();
    }

    public void setContent(int res) {
        setContent(LayoutInflater.from(getContext()).inflate(res, null));
    }

    /**
     * attach to activity ,this will remove all view in content,keep the layout empty.
     * @param activity for find the base layout
     */
    public void attachToWindow(Activity activity){
        ViewGroup contentParent = (ViewGroup)activity.findViewById(android.R.id.content);
        View content = contentParent.getChildAt(0);
        contentParent.removeView(content);
        contentParent.addView(this);
        mAboveViewContent.setContent(content);
    }

    public void setContent(View view) {
        mAboveViewContent.setContent(view);
    }

    /**
     * add SlidingMenu to Layout,so can implementation local Fling effect
     * attention:The
     * @param activity for find View,you can replace by LayoutManager
     * @param resId layout resId
     */
    public void attachToContainer(Activity activity,int resId){
        ViewGroup contentParent = (ViewGroup)activity.findViewById(resId);
        contentParent.addView(this);
    }

    public AboveViewContent getAboveViewContent(){
        return mAboveViewContent;
    }

    public void setMenuListener(SlidingMenuView.onSlidingStatusChanged listener) {
        mAboveViewContent.setMenuListener(listener);
    }

    public void setMenu(int resId){
        setMenu(LayoutInflater.from(getContext()).inflate(resId, null));
    }

    public void setMenu(View v){
        mBehindContent.setContent(v);
    }

    public void setMenuWidthOffsetRes(int resId){
        setMenuWidthOffset((int) getContext().getResources().getDimension(resId));
    }
    public void setMenuWidthOffset(int resId){
        mBehindContent.setWidthOffset(resId);
    }

    public void setSecondaryMenuOffsetRes(int resid){
        setSecondaryMenuOffset((int) getContext().getResources().getDimension(resid));
    }

    public boolean isMenuOpen(){
        return mAboveViewContent.isMenuOpen();
    }

    public void setSecondaryMenuOffset(int offset){
        mBehindContent.setmSecondaryWidthOffset(offset);
    }

    /**
     * SlidingMenu status changed callback method
     */
    public interface onSlidingStatusChanged{

        /**
         * when sliding menu open
         * @param curPage   which page is display
         */
        void onSlidingOpen(int curPage);

        /**
         * when sliding menu close
         * @param curPage   which page is display
         */
        void onSlidingClose(int curPage);
    }
}
